package br.pucrs.lognplay;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import org.apache.log4j.Logger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

import br.pucrs.lognplay.job.StaticIndexerControllerJob;
import br.pucrs.lognplay.job.StorerControllerJob;

public class Controller {

	private static final Logger logger = Logger.getLogger(Controller.class);

	private static boolean processStaticIndexerControllerJob = true;

	private static boolean processStorerControllerJob = true;

	/* StaticIndexerController */

	private static final String CONTROLLER_STATIC_INDEXER_JOB_NAME = "CONTROLLER_STATIC_INDEXER_JOB_NAME";

	private static final String CONTROLLER_STATIC_INDEXER_TRIGGER_NAME = "CONTROLLER_STATIC_INDEXER_TRIGGER_NAME";

	private static final String CONTROLLER_STATIC_INDEXER_GROUP_NAME = "CONTROLLER_STATIC_INDEXER_GROUP_NAME";

	private static final Integer CONTROLLER_STATIC_INDEXER_INTERVAL = 60; // TODO

	/* StorerController */

	private static final String CONTROLLER_STORER_JOB_NAME = "CONTROLLER_STORER_JOB_NAME";

	private static final String CONTROLLER_STORER_TRIGGER_NAME = "CONTROLLER_STORER_TRIGGER_NAME";

	private static final String CONTROLLER_STORER_GROUP_NAME = "CONTROLLER_STORER_GROUP_NAME";

	private static final Integer CONTROLLER_STORER_INTERVAL = 600;// 86400; //
																	// TODO

	public static void main(String[] arguments) {
		try {
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
			scheduler.start();

			if (processStaticIndexerControllerJob) {
				logger.trace("Initializing StaticIndexerControllerJob...");
				// @formatter:off
				JobDetail staticIndexerControllerJob = newJob(StaticIndexerControllerJob.class)
						.withIdentity(CONTROLLER_STATIC_INDEXER_JOB_NAME, CONTROLLER_STATIC_INDEXER_GROUP_NAME)
						.build();
	
				Trigger staticIndexerControllerTrigger = newTrigger()
						.withIdentity(CONTROLLER_STATIC_INDEXER_TRIGGER_NAME, CONTROLLER_STATIC_INDEXER_GROUP_NAME)
						.startNow()
						.withSchedule(simpleSchedule()
								.withIntervalInSeconds(CONTROLLER_STATIC_INDEXER_INTERVAL)
								.repeatForever())
						.build();
				// @formatter:on
				scheduler.scheduleJob(staticIndexerControllerJob, staticIndexerControllerTrigger);
				logger.trace("StaticIndexerControllerJob scheduled.");
			}

			if (processStorerControllerJob) {
				logger.trace("Initializing StorerControllerJob...");
				// @formatter:off
				JobDetail storerControllerJob = newJob(StorerControllerJob.class)
						.withIdentity(CONTROLLER_STORER_JOB_NAME, CONTROLLER_STORER_GROUP_NAME)
						.build();
	
				Trigger storerControllerTrigger = newTrigger()
						.withIdentity(CONTROLLER_STORER_TRIGGER_NAME, CONTROLLER_STORER_GROUP_NAME)
						.startNow()
						.withSchedule(simpleSchedule()
								.withIntervalInSeconds(CONTROLLER_STORER_INTERVAL)
								.repeatForever())
						.build();
				// @formatter:on
				scheduler.scheduleJob(storerControllerJob, storerControllerTrigger);
				logger.trace("StorerControllerJob scheduled.");
			}
		} catch (SchedulerException e) {
			logger.error(e);
		}
	}
}
